//------------------------------------------------------
//--                                                	--
//--				www.riemers.net			--
//--				Basic shaders			--
//--			Use/modify as you like              --
//--                                                	--
//------------------------------------------------------



//------- XNA-to-HLSL variables --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;
bool xEnableLighting;
bool xShowNormals;

//------- Texture Samplers --------

Texture xTexture;
sampler TextureSampler = sampler_state { texture = <xTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};




//------- Technique: PointSprites --------

struct VS_OUTPUT
{
	half4 Position : POSITION0;
	half2 TexCoord : TEXCOORD0;
    half4 Color : COLOR0;
	float1 Size : PSIZE;
};

struct VS_INPUT
{
	half4 Position : POSITION0;
	float1 Size : PSIZE;
	half2 Data : TEXCOORD0;
};

VS_OUTPUT VertexShader(VS_INPUT input)
{
    VS_OUTPUT Output = (VS_OUTPUT)0;
     
    float4x4 preViewProjection = mul (xView, xProjection);
	float4x4 preWorldViewProjection = mul (xWorld, preViewProjection); 
    Output.Position = mul(input.Position, preWorldViewProjection);    
    //Output.Size = 10000/(pow(Output.Position.z,2)+1	) * input.Size;
    Output.Size = input.Size * xProjection._m11/ Output.Position.w * 500 / 5;
    Output.Color.a = input.Data;
  
	if(Output.Size > 500){Output.Size = 500;}
 
    return Output;    
}
float4 AdjustSaturation(float4 color, float saturation)
{
    // The constants 0.3, 0.59, and 0.11 are chosen because the
    // human eye is more sensitive to green light, and less to blue.
    float grey = dot(color, float3(0.3, 0.59, 0.11));

    return lerp(grey, color, saturation);
}

half4 PixelShader(VS_OUTPUT input) : COLOR0
{ 
    
	half4 Color = tex2D(TextureSampler, input.TexCoord.xy);
	Color = AdjustSaturation(Color, 0.5) * 0.5;	
	Color *= (1 - saturate(Color));
	Color.a = input.Color.a;
	return Color;
}

technique PointSprites_2_0
{
	pass Pass0
	{   
		PointSpriteEnable = true;
		VertexShader = compile vs_2_0 VertexShader();
		PixelShader  = compile ps_2_0 PixelShader();
	}
}

technique PointSprites
{
	pass Pass0
	{   
		//AlphaBlendEnable = True;
		SrcBlend = srcAlpha;
		DestBlend = One;
		ZWriteEnable = false; 
  
		PointSpriteEnable = true;
		VertexShader = compile vs_1_1 VertexShader();
		PixelShader  = compile ps_1_1 PixelShader();
	}
}

